package com.nanfang51g3.ckz.ui.fragment.withdraw;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.ui.money.preview.PreviewMoneyInfoActivity;
import com.nanfang51g3.ckz.ui.money.withdraw.WithdrawContract;
import com.nanfang51g3.ckz.utils.EasyToast;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.SPUtil;
import com.nanfang51g3.ckz.widget.ClearEditText;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/4/28.
 */
public class WithdrawBankFragment extends Fragment {
    public static WithdrawContract.WithdrawActionsListener mWithdrawActionsListener;
    @Bind(R.id.tv_a_withdraw_name)
    TextView tvAWithdrawName;
    @Bind(R.id.tv_a_withdraw_id)
    TextView tvAWithdrawId;
    @Bind(R.id.cet_bank_withdraw_input)
    ClearEditText cetBankWithdrawInput;
    String bank_name;
    String bank_card;
    String deal_total;


    public static WithdrawBankFragment newInstance(Context context, WithdrawContract.WithdrawActionsListener withdrawActionsListener) {
        WithdrawBankFragment withdrawBankFragment = new WithdrawBankFragment();
        Bundle args = new Bundle();
        String bank_name = (String) SPUtil.get(context, "bank_name", "暂未填写");
        String bank_card = (String) SPUtil.get(context, "bank_card", "暂未填写");
        String deal_total = (String) SPUtil.get(context, "deal_total", "");
        args.putString("deal_total", deal_total);
        args.putString("bank_name", bank_name);
        args.putString("bank_card", bank_card);
        KLog.i("Evd", "deal_total=" + deal_total);
        KLog.i("Evd", "bank_name=" + bank_name);
        KLog.i("Evd", "bank_card=" + bank_card);
        withdrawBankFragment.setArguments(args);
        mWithdrawActionsListener = withdrawActionsListener;
        return withdrawBankFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.withdraw_bank_fragment, null);
        ButterKnife.bind(this, view);
        Bundle bundle = getArguments();
        bank_name = bundle.getString("bank_name");
        bank_card = bundle.getString("bank_card");
        deal_total = bundle.getString("deal_total");
        KLog.i("Evd", "bank_name=" + bank_name);
        KLog.i("Evd", "bank_card=" + bank_card);
        KLog.i("Evd", "deal_total=" + deal_total);
        KLog.i("Evd", mWithdrawActionsListener == null ? "mWithdrawActionsListener - 1" : "mWithdrawActionsListener - 0");
        tvAWithdrawName.setText(bank_name);
        String value = bank_card.substring(bank_card.length() - 4, bank_card.length());
        tvAWithdrawId.setText("尾号：" + value);
        cetBankWithdrawInput.setHint("可提现金额" + deal_total + "元");
        cetBankWithdrawInput.setHighlightColor(getResources().getColor(R.color.gray));
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
          bank_name = (String) SPUtil.get(getActivity(), "bank_name", "暂未填写");
          bank_card = (String) SPUtil.get(getActivity(), "bank_card", "暂未填写");
          deal_total = (String) SPUtil.get(getActivity(), "deal_total", "");
        tvAWithdrawName.setText(bank_name);
        String value = bank_card.substring(bank_card.length() - 4, bank_card.length());
        tvAWithdrawId.setText("尾号：" + value);
        cetBankWithdrawInput.setHint("可提现金额" + deal_total + "元");
        cetBankWithdrawInput.setHighlightColor(getResources().getColor(R.color.gray));

    }

    @OnClick(R.id.relative_withdraw_bank)
    public void relative_withdraw_bank(View view) {
        startActivity(new Intent(getActivity(), PreviewMoneyInfoActivity.class));
    }

    @OnClick(R.id.tv_bank_confirm_withdraw)
    public void tv_bank_confirm_withdraw(View view) {
        if (bank_name.equals("暂未填写") && bank_card.equals("暂未填写")) {
            EasyToast.showShort(getActivity(), "请填写银行卡信息");
            startActivity(new Intent(getActivity(), PreviewMoneyInfoActivity.class));
            return;
        } else {
            String total = cetBankWithdrawInput.getText().toString().trim();
            /**
             * @see  com.nanfang51g3.ckz.ui.money.withdraw.WithdrawPresenter#deal(int, String)
             */
            mWithdrawActionsListener.deal(2, total);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
